<template>
    <el-dialog v-model="dialogVisible" title="上传博客" width="500" :before-close="handleClose">
        <el-form :model="form" label-width="80px">
            <el-form-item label="标题">
                <el-input v-model="form.title" placeholder="请输入博客标题"></el-input>
            </el-form-item>
            <el-form-item label="摘要">
                <el-input type="textarea" v-model="form.summary" placeholder="请输入博客摘要"></el-input>
            </el-form-item>
            <el-form-item label="标签">
                <el-select v-model="form.tags" multiple filterable allow-create default-first-option
                    placeholder="请选择或输入标签">
                    <el-option v-for="tag in form.tags" :key="tag" :label="tag" :value="tag">
                    </el-option>
                </el-select>
            </el-form-item>
        </el-form>
        <template #footer>
            <div class="dialog-footer">
                <el-button @click="dialogVisible = false">Cancel</el-button>
                <el-button type="primary" @click="uploadBlog">
                    Confirm
                </el-button>
            </div>
        </template>
    </el-dialog>

    <el-button type="primary" @click="dialogVisible = true">上传博客</el-button>
</template>

<script lang="ts" setup>
import { EventCenter } from '@/utils/event/eventCenter';
import { useEvent } from '@/utils/event/useEvent';
import { ElMessageBox } from 'element-plus';
import { ref } from 'vue'

const dialogVisible = ref(false)
const { eventEmit } = useEvent();

const form = ref({
    title: '',
    summary: '',
    tags: <string[]>[]
});

const uploadBlog = () => {
    dialogVisible.value = false;
    eventEmit(EventCenter.UploadBlog, form.value)
}

const handleClose = async (done: () => void) => {
    try {
        await ElMessageBox.confirm('Are you sure to close this dialog?');
        done();
    } catch (error) {
        // 处理取消确认的情况
    }
}
</script>